<?php

namespace app\modules\admin\controllers;

use Yii;
use yii\web\Response;
use app\modules\admin\components\BaseAdminController;
use app\components\Api;
use app\components\Wechat;
use app\components\Loyalty;
use app\components\Log;
use yii\helpers\ArrayHelper;
use yii\data\ArrayDataProvider;
use yii\filters\AccessControl;

class MaterialController extends BaseAdminController
{

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => [],
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }

    // 图文消息列表
    public function actionListImgArticles()
    {
        $counts = Wechat::material_count();
        $news_count = $counts['news_count'];
        $page = ceil($news_count / 20);
        $img_articles = [];
        for ($i = 0; $i < $page; $i++) {
            $data = [
                'type' => 'news',
                'offset' => $i * 20,
                'count' => 20,
            ];
            $re = Wechat::material_batch_get($data);
            $img_articles = ArrayHelper::merge($img_articles, $re);
        }
        $img_articles = $img_articles['item'];
        foreach ($img_articles as $k => $v) {
            $new[$k]['media_id'] = $v['media_id'];
            $new[$k]['title'] = $v['content']['news_item'][0]['title'];
            $new[$k]['author'] = $v['content']['news_item'][0]['author'];
            $new[$k]['digest'] = $v['content']['news_item'][0]['digest'];
            $new[$k]['url'] = $v['content']['news_item'][0]['url'];
            $new[$k]['thumb_url'] = $v['content']['news_item'][0]['thumb_url'];
            $new[$k]['create_time'] = $v['content']['create_time'];
            $new[$k]['update_time'] = $v['content']['update_time'];
        }
        // echo '<pre>';print_r($new);exit;
        $dataProvider = new ArrayDataProvider([
            'allModels' => $new,
            // 'pagination' => [
            //     'pageSize' => 20,
            // ],
        ]);

        return $this->render('list-img-articles', [
            'counts' => $counts,
            'img_articles' => $img_articles,
            'dataProvider' => $dataProvider,
        ]);
    }

    public function actionTest()
    {
        $data = ['mobile' => '18483629341'];
        // $data = ['code' => '18483629341'];
        // $re = Loyalty::customer_delete($data);
        // customer_delete
        $re = Loyalty::customer_search($data);
        echo '<pre>';print_r($re);exit;
        print_r(Wechat::ips());
    }

}
